<?php

class cLog {
	public $f_log;
	public $h_log;

	public function __construct($subdir, $desc, $instance_id = "99") {
		// make sure path exists
		if(!file_exists("logs")) mkdir("logs");
		if(!file_exists("logs\\$subdir")) mkdir("logs\\$subdir");
		if(!file_exists("logs\\$subdir\\$desc")) mkdir("logs\\$subdir\\$desc");
		$path = "logs\\$subdir\\$desc";

		// generate a log filename
		$f_tmp = "$path\\". date("Y-m-d", time()) ."_$instance_id";
		$filename = $f_tmp .".log";

		// set the filename
		$this->f_log = $filename;

		// open up the log for writing
		$this->h_log = fopen($this->f_log, "a");
	}

	public function __destruct() {
		fclose($this->h_log);
	}

	public function write($data) {
		$data = rtrim($data);	// get rid of '\n' if exists
		fwrite($this->h_log, "[". date("Y-m-d h:s a", time()) ."] "
			. $data ."\n");
	}
}

?>
